<?php

namespace Admin\Controller;

class ClassController extends AdminController {

    public function index() {
        $name = I('name');
        $category = I('category', 0, 'intval');
        $tag = I('tag', 0, 'intval');
        $where = [];
        if ($name) {
            $where['name'] = ['like', "%{$name}%"];
        }
        if ($category) {
            $where['category'] = $category;
        }
        if ($tag) {
            $where['_string'] = "tag & " . pow(2, intval($tag) - 1);
        }
        $total = M('Class')->where($where)->count();
        $Page = new \Think\Page($total, 20);
        $result = M('Class')->where($where)->page($Page->nowPage, $Page->listRows)->order('sort ASC,id DESC')->select();
        foreach ($result as $key => $val) {
            $result[$key]['teacher'] = M('Teacher')->where(['id' => $val['teacher_id']])->getField('name');
            $result[$key]['category'] = M('ClassCategory')->where(['id' => $val['category']])->getField('name');
        }
        $tag_list = M('Tag')->order('sort ASC,id ASC')->select();
        $cats = M('ClassCategory')->order('sort asc,id desc')->select();
        $this->assign('tag_list', $tag_list);
        $this->assign('result', $result);
        $this->assign('page', $Page->show());
        $this->assign('cats', $cats);
        $this->display();
    }

    /**
     * 添加课程
     */
    public function add() {
        if (IS_POST) {
            $name = I('name');
            $category = I('category', 0, 'intval');
            $teacher_id = I('teacher_id', 0, 'intval');
            $level = I('level', 0, 'intval');
            $cost = I('cost', 0, 'intval');
//            $thumb1 = $_FILES['thumb1'];
            $thumb2 = $_FILES['thumb2'];
            $summary = I('summary');
            $closed = I('closed', 0, 'tinyintval');
            $hot = I('hot', 0, 'tinyintval');
            $recommend = I('hot', 0, 'tinyintval');
            $tags = I('tag', 0, 'intval');
            if (!$name) {
                $this->error('课程名称不能为空');
            }
            if (!$category) {
                $this->error('课程分类不能为空');
            }
            if (!$teacher_id) {
                $this->error('授课老师不能为空');
            }
//            if (!$level) {
//                $this->error('课程等级不能为空');
//            }
//            if (!$thumb1) {
//                $this->error('封面图不能为空');
//            }
            if (!$thumb2) {
                $this->error('封面图不能为空');
            }

//            $thumb1_url = $this->uploadImage($_FILES['thumb1']);
            $thumb2_url = $this->uploadImage($_FILES['thumb2']);
            $data = [
                'name' => $name,
                'category' => $category,
                'teacher_id' => $teacher_id,
                'level' => $level,
                'cost'  =>  $cost,
//                'thumb1' => $thumb1_url,
                'thumb2' => $thumb2_url,
                'summary' => $summary,
                'join' => 0,                
                'add_time' => time(),
                'lesson' => 0,
                'closed' => $closed,
                'hot' => $hot,
                'recommend' => $recommend,
                'sort' => 0,
            ];
            $tag = 0;
            if (!empty($tags)) {
                foreach ($tags as $i) {
                    $tag += pow(2, $i - 1);
                }
            }
            $data['tag'] = $tag;
            if (M('Class')->data($data)->add()) {
                $this->success('课程添加成功', U('class/index'));
            } else {
                $this->error('课程添加失败');
            }
        } else {
            $category = M('ClassCategory')->order('sort ASC,id ASC')->select();
            $teacher = M('Teacher')->order('id ASC')->select();
            $tag_list = M('Tag')->order('sort ASC,id ASC')->select();
            $this->assign('tag_list', $tag_list);
            $this->assign('teacher', $teacher);
            $this->assign('category', $category);
            $this->display();
        }
    }

    /**
     * 设置相关状态
     */
    public function field() {
        $id = I('id', 0, 'intval');
        $field = I('field');
        $data = I('data', 0, 'tinyintval');
        if (!in_array($field, ['hot', 'recommend', 'closed', 'free']) || !$id) {
            $this->error('参数错误');
        }
        M('Class')->where(['id' => $id])->setField($field, $data);
        $this->success('设置成功');
    }

    /**
     * 编辑课程
     */
    public function edit() {
        $id = I('id', 0, 'intval');
        if (!$id) {
            $this->error('参数错误');
        }
        if (IS_POST) {
            $name = I('name');
            $category = I('category', 0, 'intval');
            $teacher_id = I('teacher_id', 0, 'intval');
            $level = I('level', 0, 'intval');
            $cost = I('cost', 0, 'intval');
            $thumb1 = $_FILES['thumb1'];
            $thumb2 = $_FILES['thumb2'];
            $summary = I('summary');
            $free = I('free', 0, 'tinyintval');
            $closed = I('closed', 0, 'tinyintval');
            $hot = I('hot', 0, 'tinyintval');
            $recommend = I('hot', 0, 'tinyintval');
            $tags = I('tag',0,'intval');
            if (!$name) {
                $this->error('课程名称不能为空');
            }
            if (!$category) {
                $this->error('课程分类不能为空');
            }
            if (!$teacher_id) {
                $this->error('授课老师不能为空');
            }
//            if (!$level) {
//                $this->error('课程等级不能为空');
//            }


            $data = [
                'id' => $id,
                'name' => $name,
                'category' => $category,
                'teacher_id' => $teacher_id,
                'level' => $level,
                'summary' => $summary,
                'cost' => $cost,
                'closed' => $closed,
                'hot' => $hot,
                'recommend' => $recommend,
            ];

              $tag = 0;
            if (!empty($tags)) {
                foreach ($tags as $i) {
                    $tag += pow(2, $i - 1);
                }
            }
            $data['tag'] = $tag;
            if ($thumb1) {
                $thumb1_url = $this->uploadImage($_FILES['thumb1']);
                $data['thumb1'] = $thumb1_url;
            }
            if ($thumb2) {
                $thumb2_url = $this->uploadImage($_FILES['thumb2']);
                $data['thumb2'] = $thumb2_url;
            }
            if (M('Class')->data($data)->save() !== false) {
                $this->success('课程编辑成功', U('class/index'));
            } else {
                $this->error('课程编辑失败');
            }
        } else {
            $result = M('Class')->find($id);
            if (!$result) {
                $this->error('课程不存在');
            }
            $result['tag'] = parse_tags($result['tag']);
            $category = M('ClassCategory')->order('sort ASC,id ASC')->select();
            $teacher = M('Teacher')->order('id ASC')->select();
            $tag_list = M('Tag')->order('sort ASC,id ASC')->select();
            $this->assign('tag_list', $tag_list);
            $this->assign('teacher', $teacher);
            $this->assign('category', $category);
            $this->assign('info', $result);
            $this->assign('redirect_url', $_SERVER['HTTP_REFERER']);
            $this->display();
        }
    }

    /**
     * 删除课程
     */
    public function delete() {
        $id = I('id', 0, 'intval');
        M('Class')->delete($id);
        M('Lesson')->where(['class_id' => $id])->delete();
        $this->success('删除成功');
    }

    public function sort() {
        $sort = I('sort', 0, 'intval');
        if (!is_array($sort)) {
            $sort = [$sort];
        }
        foreach ($sort as $id => $val) {
            M('Class')->where(['id' => $id])->setField('sort', $val);
        }
        $this->success('操作成功');
    }

    /**
     * 课程分类
     */
    public function category() {
        $total = M('ClassCategory')->count();
        $Page = new \Think\Page($total, 20);
        $result = M('ClassCategory')->order('sort ASC,id ASC')->page($Page->nowPage, $Page->listRows)->select();
        $this->assign('page', $Page->show());
        $this->assign('result', $result);
        $this->display();
    }

    /**
     * 分类添加
     */
    public function category_add() {
        if (IS_POST) {
            $name = I('name');
            if (!$name) {
                $this->error('分类名不能为空');
            }
            $total = M('ClassCategory')->where(['name' => $name])->count();
            if ($total) {
                $this->error('分类名已经存在');
            }
            if (M('ClassCategory')->data(['name' => $name])->add()) {
                $this->success('分类添加成功');
            } else {
                $this->error('分类添加失败');
            }
        } else {
            $this->display();
        }
    }

    /**
     * 分类编辑
     */
    public function category_edit() {
        $id = I('id', 0, 'intval');
        $result = M('ClassCategory')->find($id);
        if (IS_POST) {
            $name = I('name');
            if (!$name) {
                $this->error('分类名不能为空');
            }
            $total = M('ClassCategory')->where(['name' => $name, 'id' => ['neq', $id]])->count();
            if ($total) {
                $this->error('分类名已经存在');
            }
            if (M('ClassCategory')->data(['name' => $name, 'id' => $id])->save() !== false) {
                $this->success('分类修改成功');
            } else {
                $this->error('分类修改失败');
            }
        } else {
            $this->assign($result);
            $this->display();
        }
    }

    /**
     * 分类删除
     */
    public function category_delete() {
        $id = I('id', 0, 'intval');
        M('ClassCategory')->delete($id);
        $this->success('分类删除成功');
    }

    /**
     * 分类排序
     */
    public function category_sort() {
        $sorts = I('sort');
        foreach ($sorts as $key => $val) {
            M('ClassCategory')->where(['id' => $key])->setField('sort', $val);
        }
        $this->success('操作成功');
    }

    public function category_unique() {
        $name = I('name');
        $id = I('id', 0, 'intval');
        $where = ['name' => $name];
        if ($id) {
            $where['id'] = ['neq', $id];
        }
        $total = M('ClassCategory')->where($where)->count();
        if ($total) {
            exit(json_encode(['valid' => false]));
        } else {
            exit(json_encode(['valid' => true]));
        }
    }

    /**
     * 课时
     */
    public function lesson() {
        $class_id = I('class_id', 0, 'intval');
        if (!$class_id) {
            $this->error('参数错误');
        }
        $class_info = M('Class')->find($class_id);
        if (!$class_info) {
            $this->error('课程不存在');
        }
        $total = M('Lesson')->where(['class_id' => $class_id])->count();
        $Page = new \Think\Page($total, 20);
        $result = M('Lesson')->where(['class_id' => $class_id])->page($Page->nowPage, $Page->listRows)->order('sort ASC,id DESC')->select();
        $this->assign('class_info', $class_info);
        $this->assign('class_id', $class_id);
        $this->assign('result', $result);
        $this->assign('page', $Page->show());
        $this->display();
    }

    /**
     * 添加课时
     */
    public function lesson_add() {
        $class_id = I('class_id', 0, 'intval');
        if (!$class_id) {
            $this->error('参数错误');
        }
        $class_info = M('Class')->find($class_id);
        if (!$class_info) {
            $this->error('课程不存在');
        }
        if (IS_POST) {
            $name = I('name');
            $content = I('content');
            $player = I('player');
            $source = I('source');
            $attachment = I('attachment');
            if (!$name) {
                $this->error('课时名称不能为空');
            }
            if (!$player && !$source) {
                $this->error('视频代码和视频地址至少填写一个');
            }
            $data = [
                'class_id' => $class_id,
                'name' => $name,
                'content' => $content,
                'player' => $player,
                'source' => $source,
                'attachment' => $attachment,
                'views' => 0,
                'add_time' => time(),
            ];
            if (M('Lesson')->data($data)->add()) {
                M('Class')->where(array('id'=>$class_id))->setInc('lesson');
                $this->success('课时添加成功', U('class/lesson', ['class_id' => $class_id]));
            } else {
                $this->error('课时添加失败');
            }
        } else {

            $this->assign('class_info', $class_info);
            $this->assign('class_id', $class_id);
            $this->display();
        }
    }

    public function lesson_edit() {
        $id = I('id', 0, 'intval');
        if (!$id) {
            $this->error('参数错误');
        }
        $result = M('Lesson')->find($id);
        if (!$result) {
            $this->error('课程不存在');
        }

        if (IS_POST) {
            $name = I('name');
            $content = I('content');
            $player = I('player');
            $source = I('source');
            $attachment = I('attachment');
            if (!$name) {
                $this->error('课时名称不能为空');
            }
            if (!$player && !$source) {
                $this->error('视频代码和视频地址至少填写一个');
            }
            $class_id = $result['class_id'];
            $data = [
                'name' => $name,
                'content' => $content,
                'player' => $player,
                'source' => $source,
                'attachment' => $attachment,
            ];
            if (M('Lesson')->where(['id' => $id])->data($data)->save() !== false) {
                $this->success('课时编辑成功', U('class/lesson', ['class_id' => $class_id]));
            } else {
                $this->error('课时编辑失败');
            }
        } else {
            $class_info = M('Class')->find($class_id);
            $this->assign('class_info', $class_info);
            $result['content'] = htmlspecialchars_decode($result['content']);
            $this->assign($result);
            $this->display();
        }
    }

    public function lesson_sort() {
        $sort = I('sort', 0, 'intval');
        if (!is_array($sort)) {
            $sort = [$sort];
        }
        foreach ($sort as $id => $val) {
            M('Lesson')->where(['id' => $id])->setField('sort', $val);
        }
        $this->success('操作成功');
    }

    public function lesson_delete($id) {
        $lesson = M('Lesson')->find($id);        
        M('Lesson')->delete($id);
        if($lesson){
            M('Class')->where(array('id'=>$lesson['class_id']))->setDec('lesson');
        }
        $this->success('删除成功');
    }

    public function teacher() {
        $total = M('Teacher')->count();
        $Page = new \Think\Page($total, 20);
        $result = M('Teacher')->order('id ASC')->page($Page->nowPage, $Page->listRows)->select();
        $this->assign('page', $Page->show());
        $this->assign('result', $result);
        $this->display();
    }

    public function teacher_add() {
        if (IS_POST) {
            $name = I('name');
            $intro = I('intro');
            if (!$name) {
                $this->error('分类名不能为空');
            }
            if (!$_FILES['pic']) {
                $this->error('讲师相片不能为空');
            }
            if (!$intro) {
                $this->error('讲师简介不能为空');
            }
            $pic = $this->uploadImage($_FILES['pic']);
            $data = [
                'name' => $name,
                'pic' => $pic,
                'intro' => $intro,
            ];
            if (M('Teacher')->data($data)->add()) {
                $this->success('添加成功');
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->display();
        }
    }

    public function teacher_edit() {
        $id = I('id', 0, 'intval');
        if (!$id) {
            $this->error('讲师不存在');
        }
        if (IS_POST) {
            $name = I('name');
            $intro = I('intro');
            $pic = $_FILES['pic'];
            if (!$name) {
                $this->error('分类名不能为空');
            }

            if (!$intro) {
                $this->error('讲师简介不能为空');
            }

            $data = [
                'id' => $id,
                'name' => $name,
                'intro' => $intro,
            ];
            if ($pic) {
                $pic = $this->uploadImage($_FILES['pic']);
                $data['pic'] = $pic;
            }
            if (M('Teacher')->data($data)->save()) {
                $this->success('编辑成功');
            } else {
                $this->error('编辑成功');
            }
        } else {
            $result = M('Teacher')->find($id);
            $this->assign($result);
            $this->display();
        }
    }

    public function teacher_delete() {
        $id = I('id', 0, 'intval');
        M('Teacher')->delete($id);
        $this->success('删除成功');
    }

    public function tag() {
        $result = M('Tag')->order('sort ASC,id ASC')->select();
        foreach ($result as $key => $val) {
            $result[$key]['total'] = M('Class')->where("tag & " . pow(2, intval($val['id']) - 1))->count();
        }
        $this->assign('result', $result);
        $this->display();
    }

    public function tag_add() {
        if (IS_POST) {
            $name = I('name');
            $category = I('category');
            if (!$name) {
                $this->error('请输入标签名');
            }
            M('Tag')->data(['name' => $name,'category'=>$category])->add();
            $this->success('添加成功');
        } else {
            $this->display();
        }
    }

    public function tag_edit() {
        $id = I('id', 0, 'intval');
        if (IS_POST) {
            $name = I('name');
            $category = I('category');
            if (!$name) {
                $this->error('请输入标签名');
            }
            M('Tag')->data(['name' => $name,'category'=>$category])->where(['id' => $id])->save();
            $this->success('编辑成功');
        } else {
            $result = M('Tag')->find($id);
            $this->assign($result);
            $this->display();
        }
    }

    public function tag_delete() {
        $id = I('id', 0, 'intval');
        M('Tag')->delete($id);
        $this->success('删除成功');
    }

    public function tag_sort() {
        $sort = I('sort', 0, 'intval');
        if (!is_array($sort)) {
            $sort = [$sort];
        }
        foreach ($sort as $id => $val) {
            M('Tag')->where(['id' => $id])->setField('sort', $val);
        }
        $this->success('操作成功');
    }

}
